package com.lzy.chat.f_config;

import com.lzy.chat.c_service.WebsocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WebsocketConfig implements WebSocketConfigurer {


    private WebsocketService websocketService;

    @Bean
    public HandshakeInterceptor handshakeInterceptor() {
        return new HandshakeInterceptor();
    }

    @Autowired
    public void setWebsocketController(WebsocketService websocketService) {
        this.websocketService = websocketService;
    }

    //设置handler和拦截器
    //地址为  ws://.../ws
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(websocketService, "ws")
                .addInterceptors(handshakeInterceptor())
                .setAllowedOrigins("*");
    }
}
